<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Go by Example 中文版: 字符串函数</title>
    <link rel=stylesheet href="site.css">
  </head>
  <script>
      onkeydown = (e) => {
          
          if (e.key == "ArrowLeft") {
              window.location.href = 'collection-functions';
          }
          
          
          if (e.key == "ArrowRight") {
              window.location.href = 'string-formatting';
          }
          
      }
  </script>
  <body>

    <div class="example" id="string-functions">
      <h2><a href="./">Go by Example 中文版</a>: 字符串函数</h2>
      
      <table>
        
        <tr>
          <td class="docs">
            <p>标准库的 <code>strings</code> 包提供了很多有用的字符串相关的函数。
这儿有一些用来让你对 <code>strings</code> 包有一个初步了解的例子。</p>

          </td>
          <td class="code empty leading">
            
          
          </td>
        </tr>
        
        <tr>
          <td class="docs">
            
          </td>
          <td class="code leading">
            <a href="http://play.golang.org/p/L7EzAmaG41B"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
          <div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            
          </td>
          <td class="code leading">
            
          <div class="highlight"><pre><span class="kn">import</span> <span class="p">(</span>
    <span class="s">&quot;fmt&quot;</span>
    <span class="nx">s</span> <span class="s">&quot;strings&quot;</span>
<span class="p">)</span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            <p>我们给 <code>fmt.Println</code> 一个较短的别名，
因为我们随后会大量的使用它。</p>

          </td>
          <td class="code leading">
            
          <div class="highlight"><pre><span class="kd">var</span> <span class="nx">p</span> <span class="p">=</span> <span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            
          </td>
          <td class="code leading">
            
          <div class="highlight"><pre><span class="kd">func</span> <span class="nx">main</span><span class="p">()</span> <span class="p">{</span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            <p>这是一些 <code>strings</code> 中有用的函数例子。
由于它们都是包的函数，而不是字符串对象自身的方法，
这意味着我们需要在调用函数时，将字符串作为第一个参数进行传递。
你可以在 <a href="http://golang.org/pkg/strings/"><code>strings</code></a> 包文档中找到更多的函数。</p>

          </td>
          <td class="code leading">
            
          <div class="highlight"><pre>    <span class="nx">p</span><span class="p">(</span><span class="s">&quot;Contains:  &quot;</span><span class="p">,</span> <span class="nx">s</span><span class="p">.</span><span class="nx">Contains</span><span class="p">(</span><span class="s">&quot;test&quot;</span><span class="p">,</span> <span class="s">&quot;es&quot;</span><span class="p">))</span>
    <span class="nx">p</span><span class="p">(</span><span class="s">&quot;Count:     &quot;</span><span class="p">,</span> <span class="nx">s</span><span class="p">.</span><span class="nx">Count</span><span class="p">(</span><span class="s">&quot;test&quot;</span><span class="p">,</span> <span class="s">&quot;t&quot;</span><span class="p">))</span>
    <span class="nx">p</span><span class="p">(</span><span class="s">&quot;HasPrefix: &quot;</span><span class="p">,</span> <span class="nx">s</span><span class="p">.</span><span class="nx">HasPrefix</span><span class="p">(</span><span class="s">&quot;test&quot;</span><span class="p">,</span> <span class="s">&quot;te&quot;</span><span class="p">))</span>
    <span class="nx">p</span><span class="p">(</span><span class="s">&quot;HasSuffix: &quot;</span><span class="p">,</span> <span class="nx">s</span><span class="p">.</span><span class="nx">HasSuffix</span><span class="p">(</span><span class="s">&quot;test&quot;</span><span class="p">,</span> <span class="s">&quot;st&quot;</span><span class="p">))</span>
    <span class="nx">p</span><span class="p">(</span><span class="s">&quot;Index:     &quot;</span><span class="p">,</span> <span class="nx">s</span><span class="p">.</span><span class="nx">Index</span><span class="p">(</span><span class="s">&quot;test&quot;</span><span class="p">,</span> <span class="s">&quot;e&quot;</span><span class="p">))</span>
    <span class="nx">p</span><span class="p">(</span><span class="s">&quot;Join:      &quot;</span><span class="p">,</span> <span class="nx">s</span><span class="p">.</span><span class="nx">Join</span><span class="p">([]</span><span class="kt">string</span><span class="p">{</span><span class="s">&quot;a&quot;</span><span class="p">,</span> <span class="s">&quot;b&quot;</span><span class="p">},</span> <span class="s">&quot;-&quot;</span><span class="p">))</span>
    <span class="nx">p</span><span class="p">(</span><span class="s">&quot;Repeat:    &quot;</span><span class="p">,</span> <span class="nx">s</span><span class="p">.</span><span class="nx">Repeat</span><span class="p">(</span><span class="s">&quot;a&quot;</span><span class="p">,</span> <span class="mi">5</span><span class="p">))</span>
    <span class="nx">p</span><span class="p">(</span><span class="s">&quot;Replace:   &quot;</span><span class="p">,</span> <span class="nx">s</span><span class="p">.</span><span class="nx">Replace</span><span class="p">(</span><span class="s">&quot;foo&quot;</span><span class="p">,</span> <span class="s">&quot;o&quot;</span><span class="p">,</span> <span class="s">&quot;0&quot;</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">))</span>
    <span class="nx">p</span><span class="p">(</span><span class="s">&quot;Replace:   &quot;</span><span class="p">,</span> <span class="nx">s</span><span class="p">.</span><span class="nx">Replace</span><span class="p">(</span><span class="s">&quot;foo&quot;</span><span class="p">,</span> <span class="s">&quot;o&quot;</span><span class="p">,</span> <span class="s">&quot;0&quot;</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span>
    <span class="nx">p</span><span class="p">(</span><span class="s">&quot;Split:     &quot;</span><span class="p">,</span> <span class="nx">s</span><span class="p">.</span><span class="nx">Split</span><span class="p">(</span><span class="s">&quot;a-b-c-d-e&quot;</span><span class="p">,</span> <span class="s">&quot;-&quot;</span><span class="p">))</span>
    <span class="nx">p</span><span class="p">(</span><span class="s">&quot;ToLower:   &quot;</span><span class="p">,</span> <span class="nx">s</span><span class="p">.</span><span class="nx">ToLower</span><span class="p">(</span><span class="s">&quot;TEST&quot;</span><span class="p">))</span>
    <span class="nx">p</span><span class="p">(</span><span class="s">&quot;ToUpper:   &quot;</span><span class="p">,</span> <span class="nx">s</span><span class="p">.</span><span class="nx">ToUpper</span><span class="p">(</span><span class="s">&quot;test&quot;</span><span class="p">))</span>
    <span class="nx">p</span><span class="p">()</span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            <p>虽然不是 <code>strings</code> 的函数，但仍然值得一提的是，
获取字符串长度（以字节为单位）以及通过索引获取一个字节的机制。</p>

          </td>
          <td class="code leading">
            
          <div class="highlight"><pre>    <span class="nx">p</span><span class="p">(</span><span class="s">&quot;Len: &quot;</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="s">&quot;hello&quot;</span><span class="p">))</span>
    <span class="nx">p</span><span class="p">(</span><span class="s">&quot;Char:&quot;</span><span class="p">,</span> <span class="s">&quot;hello&quot;</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
<span class="p">}</span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            <p>注意，上面的 <code>len</code> 以及索引工作在字节级别上。
Go 使用 UTF-8 编码字符串，因此通常按原样使用。
如果您可能使用多字节的字符，则需要使用可识别编码的操作。
详情请参考 <a href="https://blog.golang.org/strings">strings, bytes, runes and characters in Go</a>。</p>

          </td>
          <td class="code empty">
            
          
          </td>
        </tr>
        
      </table>
      
      <table>
        
        <tr>
          <td class="docs">
            
          </td>
          <td class="code leading">
            
          <div class="highlight"><pre><span class="gp">$</span> go run string-functions.go
<span class="go">Contains:   true</span>
<span class="go">Count:      2</span>
<span class="go">HasPrefix:  true</span>
<span class="go">HasSuffix:  true</span>
<span class="go">Index:      1</span>
<span class="go">Join:       a-b</span>
<span class="go">Repeat:     aaaaa</span>
<span class="go">Replace:    f00</span>
<span class="go">Replace:    f0o</span>
<span class="go">Split:      [a b c d e]</span>
<span class="go">ToLower:    test</span>
<span class="go">ToUpper:    TEST</span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            
          </td>
          <td class="code">
            
          <div class="highlight"><pre><span class="go">Len:  5</span>
<span class="go">Char: 101</span>
</pre></div>

          </td>
        </tr>
        
      </table>
      
      
      <p class="next">
        下一个例子: <a href="string-formatting">字符串格式化</a>
      </p>
      
      <p class="footer">
        <a href="https://twitter.com/mmcgrana">@mmcgrana</a> 编写 | <a href="https://github.com/gobyexample-cn">gobyexample-cn</a> 翻译 | <a href="https://github.com/gobyexample-cn/gobyexample/issues">反馈</a> | <a href="https://github.com/gobyexample-cn/gobyexample">源码</a> | <a href="https://github.com/mmcgrana/gobyexample#license">license</a>      </p>
      </p>
    </div>
    <script>
      var codeLines = [];
      codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A    \"fmt\"\u000A    s \"strings\"\u000A)\u000A');codeLines.push('var p = fmt.Println\u000A');codeLines.push('func main() {\u000A');codeLines.push('    p(\"Contains:  \", s.Contains(\"test\", \"es\"))\u000A    p(\"Count:     \", s.Count(\"test\", \"t\"))\u000A    p(\"HasPrefix: \", s.HasPrefix(\"test\", \"te\"))\u000A    p(\"HasSuffix: \", s.HasSuffix(\"test\", \"st\"))\u000A    p(\"Index:     \", s.Index(\"test\", \"e\"))\u000A    p(\"Join:      \", s.Join([]string{\"a\", \"b\"}, \"-\"))\u000A    p(\"Repeat:    \", s.Repeat(\"a\", 5))\u000A    p(\"Replace:   \", s.Replace(\"foo\", \"o\", \"0\", -1))\u000A    p(\"Replace:   \", s.Replace(\"foo\", \"o\", \"0\", 1))\u000A    p(\"Split:     \", s.Split(\"a-b-c-d-e\", \"-\"))\u000A    p(\"ToLower:   \", s.ToLower(\"TEST\"))\u000A    p(\"ToUpper:   \", s.ToUpper(\"test\"))\u000A    p()\u000A');codeLines.push('    p(\"Len: \", len(\"hello\"))\u000A    p(\"Char:\", \"hello\"[1])\u000A}\u000A');codeLines.push('');codeLines.push('');codeLines.push('');
    </script>
    <script src="site.js" async></script>
  </body>
</html>
